(function() {
  "use strict";
  APP.Views.DialogComManagementView = Backbone.View.extend({
    el: '#modal',
    events : {
      'submit #userPulpitListForm': 'save'
    },
    initialize: function(params) {
      this.menuView = params.menuView;
      var callback = {
        obj: this,
        name: 'getUserPulpitListSuccess'
      }
      APP.BeClient.getUserPulpitList(callback);
    },
    getUserPulpitListSuccess: function(data) {
      this.render(data.applications);
    },
    save: function(e) {
      e.preventDefault();
      var applicationIds = [];
      _.each(this.$('.appVisible:checked'), function(e) {
        applicationIds.push(parseInt($(e).val()));
      });
      var callback = {
        obj: this.menuView,
        name: 'saveUserPulpitVisibilitiesSuccess'
      }
      APP.BeClient.saveUserPulpitVisibilities(applicationIds, callback, undefined, {preloaderHide: false});
      $('#modal').modal('hide');
    },
    render: function(applications) {
      this.$el.html(_.template($('#dialogComManagementTemplate').html(), {
        applications: applications
      }));
      this.$('input').iCheck({
        checkboxClass: 'icheckbox_brands-vgp',
        radioClass: 'iradio_brands-vgp',
        increaseArea: '20%' // optional
      });
      this.$el.modal();
      return this;
    }
  });
}());